package com.wantup.miaosha.utils;

import com.alibaba.fastjson.JSON;
import com.wantup.miaosha.config.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
public class RedisUtils {

    @Autowired
    private RedisConfig redisConfig;

    @Autowired
    private JedisPool jedisPool;

    public <T> T get(String key, Class<T> zClass) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            T t = stringToBean(value, zClass);
            return t;
        } finally {
            returnToPool(jedis);
        }
    }


    public <T> boolean set(String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            if (str == null || str.length() <= 0) {
                return false;
            }
            jedis.set(key, str);
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    private <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> aClass = value.getClass();
        if (aClass == int.class || aClass == Integer.class) {
            return "" + value;
        } else if (aClass == long.class || aClass == Long.class) {
            return "" + value;
        } else if (aClass == String.class) {
            return (String) value;
        }

        return JSON.toJSONString(value);
    }


    @SuppressWarnings("unchecked")
    private <T> T stringToBean(String value, Class<T> zClass) {
        //参数校验 必不可少
        if (value == null || value.length() <= 0 || zClass == null) {
            return null;
        }
        if (zClass == int.class || zClass == Integer.class) {
            return (T)Integer.valueOf(value);
        } else if (zClass == long.class || zClass == Long.class) {
            return (T)Long.valueOf(value);
        } else if (zClass == String.class) {
            return (T) value;
        } else {
            return JSON.toJavaObject(JSON.parseObject(value),zClass);
        }


    }

    private void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    @Bean
    public JedisPool jedisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisConfig.getMaxActive());
        jedisPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisConfig.getTimeout());

        JedisPool jedisPool = new JedisPool(jedisPoolConfig,
                redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout(),
                redisConfig.getPassword(), 0);
        return jedisPool;
    }


}
